package com.zhny.project.count.algorithm.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.count.algorithm.domain.Algorithm;
import com.zhny.project.count.algorithm.service.IAlgorithmService;
import com.zhny.project.count.display.domain.Display;
import com.zhny.project.count.display.service.IDisplayService;
import com.zhny.project.count.ification.domain.Ification;
import com.zhny.project.count.ification.service.IIficationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 算法Controller
 * 
 * @author lxh
 * @date 2019-09-24
 */
@Controller
@RequestMapping("/count/algorithm")
public class AlgorithmController extends BaseController
{
    private String prefix = "count/algorithm";

    @Autowired
    private IAlgorithmService algorithmService;
    @Autowired
    private IIficationService ificationService;
    @Autowired
    private IDisplayService displayService;

    @RequiresPermissions("count:algorithm:view")
    @GetMapping()
    public String algorithm(Model model)
    {
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        model.addAttribute("ification",ifications);
        List<Display> displays = displayService.selectDisplayPage(new Display());
        model.addAttribute("display",displays);
        return prefix + "/algorithm";
    }

    /**
     * 查询算法列表
     */
    @RequiresPermissions("count:algorithm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Algorithm algorithm)
    {
        startPage();
        List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
        return getDataTable(list);
    }

    /**
     * 导出算法列表
     */
    @RequiresPermissions("count:algorithm:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Algorithm algorithm)
    {
        List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
        ExcelUtil<Algorithm> util = new ExcelUtil<Algorithm>(Algorithm.class);
        return util.exportExcel(list, "algorithm");
    }

    /**
     * 新增算法
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        model.addAttribute("ification",ifications);
        List<Display> displays = displayService.selectDisplayPage(new Display());
        model.addAttribute("display",displays);
        return prefix + "/add";
    }

    /**
     * 新增保存算法
     */
    @RequiresPermissions("count:algorithm:add")
    @Log(title = "算法", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Algorithm algorithm)
    {

        return toAjax(algorithmService.insertAlgorithm(algorithm));
    }

    /**
     * 修改算法
     */
    @GetMapping("/edit/{algorithmKey}")
    public String edit(@PathVariable("algorithmKey") String algorithmKey, ModelMap mmap)
    {
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        mmap.put("ification",ifications);
        List<Display> displays = displayService.selectDisplayPage(new Display());
        mmap.put("display",displays);
        Algorithm algorithm = algorithmService.selectAlgorithmById(algorithmKey);
        mmap.put("algorithm", algorithm);
        return prefix + "/edit";
    }

    /**
     * 修改保存算法
     */
    @RequiresPermissions("count:algorithm:edit")
    @Log(title = "算法", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Algorithm algorithm)
    {
        return toAjax(algorithmService.updateAlgorithm(algorithm));
    }

    /**
     * 删除算法
     */
    @RequiresPermissions("count:algorithm:remove")
    @Log(title = "算法", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(algorithmService.deleteAlgorithmByIds(ids));
    }

    /**
     * 详细
     */
    @GetMapping("/detail/{algorithmKey}")
    public String detail(@PathVariable("algorithmKey") String algorithmKey, ModelMap mmap)
    {
        Algorithm algorithm = algorithmService.selectAlgorithmById(algorithmKey);
        mmap.put("algorithm", algorithm);
        return prefix + "/all";
    }
    /**
     * 校验ID
     */
    @PostMapping("/checkalgorithmId")
    @ResponseBody
    public String checkalgorithmId(Algorithm algorithm)
    {
        return algorithmService.checkalgorithmId(algorithm);
    }
    /**
     * 校验Name
     */
    @PostMapping("/checkalgorithmName")
    @ResponseBody
    public String checkalgorithmName(Algorithm algorithm)
    {
        return algorithmService.checkalgorithmName(algorithm);
    }


    @GetMapping("/all")
    @ResponseBody
    public Map<String,Object> getAllCityJson(Algorithm algorithm){
        List<Algorithm> algorithms = algorithmService.selectAlgorithmList(algorithm);

        Map<String,Object> result = new HashMap<>();
        result.put("algorithms",algorithms);
        return  result;
    }
    @GetMapping("/app")
    @ResponseBody
    public Map<String,Object> getappCityJson(Algorithm algorithm){
    Algorithm algorithms = algorithmService.selectAlgorithmById(algorithm.getAlgorithmKey());
        Map<String,Object> result = new HashMap<>();
        result.put("algorithms",algorithms);
        return  result;
    }
}
